package com.tiocloud.chat.feature.curr.detail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.tiocloud.chat.R;
import com.tiocloud.chat.feature.curr.detail.mvp.CurrInfoContract;
import com.tiocloud.chat.feature.curr.detail.mvp.CurrInfoPresenter;
import com.tiocloud.chat.feature.account.pwd.ModifyPwdActivity;
import com.tiocloud.chat.feature.curr.modify.ModifyActivity;
import com.tiocloud.chat.feature.curr.modify.model.ModifyType;
import com.tiocloud.chat.databinding.TioCurrInfoActivityBinding;
import com.watayouxiang.httpclient.model.response.UserCurrResp;
import com.watayouxiang.androidutils.page.TioActivity;
import com.tiocloud.chat.util.StringUtil;

/**
 * author : TaoWang
 * date : 2020/3/12
 * desc : 个人信息页
 */
public class CurrDetailActivity extends TioActivity implements CurrInfoContract.View {

    private TioCurrInfoActivityBinding binding;
    private CurrInfoPresenter presenter;

    public static void start(Context context) {
        Intent starter = new Intent(context, CurrDetailActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.tio_curr_info_activity);
        binding.titleBar.setTitle("个人资料");
        presenter = new CurrInfoPresenter(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        presenter.getAvatarDialog().onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.updateUIData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }

    @Override
    public void onUserCurrResp(UserCurrResp userCurr) {
        binding.hivAvatar.tio_roundAvatar(userCurr.avatar);
        binding.tvNick.setText(StringUtil.nonNull(userCurr.nick));
        binding.tvGender.setText(StringUtil.nonNull(userCurr.getSex()));
        binding.tvSign.setText(StringUtil.nonNull(userCurr.sign));
        binding.tvRegion.setText(StringUtil.nonNull(userCurr.getRegion()));
        binding.tvEmail.setText(StringUtil.nonNull(userCurr.email));
        binding.tvPhone.setText(StringUtil.nonNull(userCurr.phone));

        binding.rlModifyPwd.setOnClickListener(view -> ModifyPwdActivity.start(getActivity()));
        binding.rlNick.setOnClickListener(view -> ModifyActivity.start_curr(view.getContext(), ModifyType.CURR_NICK, userCurr.nick));
        binding.rlSign.setOnClickListener(view -> ModifyActivity.start_curr(view.getContext(), ModifyType.CURR_SIGN, userCurr.sign));
        binding.rlGender.setOnClickListener(view -> presenter.showGenderDialog(view.getContext(), userCurr.sex));
        binding.vgAvatar.setOnClickListener(view -> presenter.getAvatarDialog().show());
    }
}
